package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Signin;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.SigninQueryObject;
import cn.wolfcode.crm.service.ISigninService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;


/**
 * @author konki
 * @time 2018年6月19日 下午7:03:47
 * @email konki@qq.com
 * @description
 */
@Controller
@RequestMapping("signin")
public class SigninController {
    @Autowired
    private ISigninService service;

    @RequestMapping("")
    @PermissionName("签到首页")
    @RequiresPermissions("signin:index")
    public String index() {
        return "signin/list";
    }

    @RequestMapping("list")
    @PermissionName("签到列表")
    @RequiresPermissions("signin:list")
    @ResponseBody
    public PageResult list(SigninQueryObject qo) {
        return service.query(qo);
    }

    @RequestMapping("indate")
    @PermissionName("签到")
    @RequiresPermissions("signin:indate")
    @ResponseBody
    public JsonResult indate() {
        JsonResult jsonResult = new JsonResult();
        try {
            Signin signin = new Signin();
            signin.setIndate(new Date());
            signin.setStatus(Signin.SIGNIN);
            Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
            signin.getEmployees().setId(employee.getId());
            service.insert(signin);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.markMsg("签到失败");
            return jsonResult;
        }
        return jsonResult;
    }

    @RequestMapping("outdate")
    @PermissionName("退签")
    @RequiresPermissions("signin:outdate")
    @ResponseBody
    public JsonResult outdate() {
        JsonResult jsonResult = new JsonResult();
        try {
            Signin signin = new Signin();
            signin.setOutdate(new Date());
            signin.setStatus(Signin.SIGNOUT);
            Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
            signin.getEmployees().setId(employee.getId());
            service.updateByEid(signin);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.markMsg("退签失败");
            return jsonResult;
        }
        return jsonResult;
    }
}
